package implementation.data.records;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import implementation.server.IReadOnlyThread;


public class CThreadRecord implements IReadOnlyThread, Cloneable{
	
	private String _threadTopic;
	private String _threadContent;
	private int _authorID;
	private int _threadID;
	private List <Integer> _myPosts;
	private int _forumID;
	

	public CThreadRecord(int threadID, String threadTopic, String threadContent,int authorID, int forumID){
		this._threadID = threadID;
		this._authorID = authorID;
		this._threadContent = threadContent;
		this._threadTopic = threadTopic;
		this._myPosts = Collections.synchronizedList(new ArrayList<Integer>());
		this._forumID = forumID;
	}
	
	
	public String getTopic(){
		return this._threadTopic;
	}
	public String getThreadContent(){
		return this._threadContent;
	}
	public int getAuthorID(){
		return this._authorID;
	}
	public int getThreadId(){
		return this._threadID;
	}
	
	public ArrayList<Integer> getPosts(){
		
		ArrayList<Integer> tAnswer= new ArrayList<Integer>();
		
		synchronized(this){
			int tTo = this._myPosts.size();
			for(int index=0; index < tTo; index++)
				tAnswer.add(this._myPosts.get(index));
		}

		return tAnswer;		
		

	}
	

	public boolean addPost(int postId){
		
		return this._myPosts.add(postId);
	}
	
	
	
	public String toString(){
		String tAnswer="Thread Id: "+this._threadID+ "\n"+"Thread Topic: "+this._threadTopic+"\n"+ "Thread Content: "+ this._threadContent+"\n" +"Posts Ids: "+getPosts()+ "\n"+"Author Id: "+ this._authorID ;
		
		return tAnswer;
	}

	public CThreadRecord clone(){
		
		CThreadRecord tCThreadRecord = new CThreadRecord(this._threadID, this._threadTopic, this._threadContent, this._authorID, this._forumID);
		synchronized(this){
			int tTo = this._myPosts.size();
			for(int index=0; index < tTo; index++)
				tCThreadRecord.addPost(this._myPosts.get(index));
		}
		return tCThreadRecord;
	}


	@Override
	public int getID() {
		return this._threadID;
	}


	@Override
	public int getForumID() {
		return this._forumID;
	}
}
